﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes.sim {
    import flash.geom.*;
    import Engine.Helpers.*;
    import flash.utils.*;
    import Classes.*;
    import Engine.*;
    import com.adobe.utils.*;

    public class POIManager implements IGameWorldStateObserver {

        private var m_pois:Dictionary;
        private var m_poiTables:Dictionary;
        private var m_resources:Dictionary;
        private var m_npcs:Dictionary;

        public function POIManager(){
            this.m_poiTables = new Dictionary();
            this.m_resources = new Dictionary();
            this.m_pois = new Dictionary();
            this.m_npcs = new Dictionary();
        }
        public function initialize():void{
        }
        public function onGameLoaded(_arg1:Object):void{
        }
        public function onResourceChange(_arg1:MapResource, _arg2:Vector3, _arg3:Vector3):void{
        }
        public function cleanUp():void{
            this.m_poiTables = null;
            this.m_resources = null;
            this.m_pois = null;
            this.m_npcs = null;
        }
        public function initializePOIs(_arg1:Array):void{
            var _local2:MapResource;
            for each (_local2 in _arg1) {
                this.addPOI(_local2);
            };
        }
        public function addPOI(_arg1:MapResource):void{
            var _local9:XML;
            var _local2:Item = _arg1.getItem();
            if (_local2.xml.poiInfo.length() == 0){
                return;
            };
            var _local3:XML = _local2.xml.poiInfo[0];
            var _local4:int = _local3.@attraction;
            var _local5:int = _local3.@influenceRadius;
            var _local6:int = _local3.@maxNPCs;
            var _local7:int = _local3.@maxLoiterActions;
            var _local8:POI = new POI(_arg1, _local4, _local5, _local6, _local7);
            for each (_local9 in _local3.action) {
                _local8.addAction(_local9.@name);
            };
            if (this.m_pois == null){
                this.m_pois = new Dictionary(true);
            };
            this.m_pois[_arg1] = _local8;
            this.updatePOI(_local8.resource);
        }
        public function removePOI(_arg1:MapResource):void{
            if (this.m_pois[_arg1] != null){
                delete this.m_pois[_arg1];
                this.m_poiTables = null;
                this.m_poiTables = new Dictionary();
            };
            if (this.m_resources[_arg1] != null){
                delete this.m_poiTables[_arg1];
            };
        }
        public function updatePOI(_arg1:MapResource):void{
            if (((!((this.m_pois[_arg1] == null))) && (this.m_poiTables))){
                this.m_poiTables = null;
                this.m_poiTables = new Dictionary();
            };
            if (this.m_resources[_arg1] != null){
                delete this.m_poiTables[_arg1];
            };
        }
        public function selectPOIFromOriginForNPC(_arg1:MapResource, _arg2:NPC):POI{
            var _local6:POI;
            if (this.m_poiTables[_arg1] == null){
                this.buildPOITable(_arg1);
                this.m_resources[_arg1] = _arg1;
            };
            var _local3:Number = Math.random();
            var _local4:POI;
            var _local5:Number = 0;
            for each (_local6 in this.m_poiTables[_arg1]) {
                if ((((_local3 <= (_local6.probability + _local5))) && (this.addNPC(_arg2, _local6)))){
                    _local4 = _local6;
                    break;
                };
                _local5 = (_local5 + _local6.probability);
            };
            return (_local4);
        }
        public function getAllPOIs():Array{
            return (DictionaryUtil.getKeys(this.m_pois));
        }
        public function getResourcesByPredicate(_arg1:Function):Array{
            var _local3:Object;
            var _local4:POI;
            var _local2:Array = new Array();
            for (_local3 in this.m_pois) {
                _local4 = this.m_pois[_local3];
                if (_arg1(_local4)){
                    _local2.push(_local3);
                };
            };
            return (_local2);
        }
        private function buildPOITable(_arg1:MapResource):void{
            var _local4:POI;
            var _local5:Point;
            var _local6:Point;
            var _local7:Number;
            var _local8:POI;
            var _local2:Array = new Array();
            var _local3:int;
            for each (_local4 in this.m_pois) {
                if (_local4.resource.isActive == false){
                } else {
                    _local5 = new Point(_arg1.getPosition().x, _arg1.getPosition().y);
                    _local6 = new Point(_local4.resource.getPosition().x, _local4.resource.getPosition().y);
                    _local7 = MathUtil.distance(_local5, _local6);
                    if (_local7 > _local4.influenceRadius){
                    } else {
                        _local8 = _local4.clone();
                        _local8.calculateAttraction(_local7);
                        _local3 = (_local3 + _local8.attraction);
                        _local2.push(_local8);
                    };
                };
            };
            for each (_local4 in _local2) {
                _local4.setProbability(_local3);
            };
            _local2.sort(this.sortByProbability);
            this.m_poiTables[_arg1] = _local2;
        }
        private function sortByProbability(_arg1:POI, _arg2:POI):int{
            return ((_arg1.probability - _arg2.probability));
        }
        private function addNPC(_arg1:NPC, _arg2:POI):Boolean{
            if (this.m_npcs[_arg2.resource.getId()] == null){
                this.m_npcs[_arg2.resource.getId()] = new Array();
                this.m_npcs[_arg2.resource.getId()].push(_arg1.getId());
                return (true);
            };
            if (this.m_npcs[_arg2.resource.getId()].length < _arg2.maxNPCs){
                this.m_npcs[_arg2.resource.getId()].push(_arg1.getId());
                return (true);
            };
            return (false);
        }
        public function removeNPC(_arg1:NPC, _arg2:POI):void{
            if (this.m_npcs[_arg2.resource.getId()] == null){
                return;
            };
            this.m_npcs[_arg2.resource.getId()].pop();
        }
        public function clearAllNPCs(_arg1:MapResource):void{
            if (this.m_pois[_arg1] != null){
                this.m_npcs[_arg1.getId()] = new Array();
            };
        }

    }
}//package Classes.sim 
